import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_excel(r"C:\Users\Administrator\PycharmProjects\PythonProject1\data\bwic.xlsx", sheet_name=0)
df_weekday = df[['created_by', 'created_time']].copy()
df_weekday['created_time'] = pd.to_datetime(df_weekday['created_time'], format='%Y%m%d')
df_weekday['weekday'] = df_weekday.created_time.dt.dayofweek + 1

x_var = 'created_by'
groupby_var = 'weekday'
df_weekday_agg = df_weekday.loc[:, [x_var, groupby_var]].groupby(groupby_var)
# df_weekday_agg = df_weekday.loc[x_var, groupby_var].groupby(groupby_var)
# print(type(df_weekday_agg))
vals = [d[x_var].values.tolist() for i, d in df_weekday_agg]
print(vals)
# Draw
plt.figure(figsize=(16, 9), dpi=80)
colors = [plt.cm.Spectral(i / float(len(vals) - 1)) for i in range(len(vals))]
n, bins, patches = plt.hist(vals, df_weekday[x_var].unique().__len__(), stacked=False, density=False,
                            color=colors[:len(vals)])

# Decoration
plt.legend(
    {group: col for group, col in zip(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'], colors[: len(vals)])})
plt.title(f"Trader activities", fontsize=22)
plt.xlabel(x_var, fontsize=16)
plt.ylabel("Frequency", fontsize=16)
plt.ylim(0, 600)
plt.xticks(ticks=bins[:7] + 0.2, labels=np.unique(df[x_var]).tolist(), horizontalalignment='left', fontsize=16)
plt.show()
